﻿using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Hx.ADSyncPlatform.Infrastructure.Utils.Extensions.BaseType
{
    public static class TaskExtensions
    {
        public static Task Catch(this Task task)
        {
            return task.ContinueWith<Task>(delegate (Task t)
            {
                if (t != null && t.IsFaulted)
                {
                    AggregateException exception = t.Exception;
                    Trace.TraceError("Catch exception thrown by Task: {0}", new object[]
                    {
                        exception
                    });
                }
                return t;
            }).Unwrap();
        }
        public static Task<T> Catch<T>(this Task<T> task)
        {
            return task.ContinueWith<Task<T>>(delegate (Task<T> t)
            {
                if (t != null && t.IsFaulted)
                {
                    AggregateException exception = t.Exception;
                    Trace.TraceError("Catch<T> exception thrown by Task: {0}", new object[]
                    {
                        exception
                    });
                }
                return t;
            }).Unwrap<T>();
        }
    }
}
